package main

import (
	"fmt"
)

type Node struct {
	Value       int
	Left, Right *Node
}

// 为treeNode结构体添加函数
func (node Node) Print() {
	fmt.Print(node.Value)
}

func main() {
	var root Node
	//		3
	//	   / \
	//	  0   5
	//	   \ /
	//	   2 4
	root = Node{Value: 3}
	root.Left = &Node{}
	root.Right = &Node{5, nil, nil}
	root.Left.Right = &Node{Value: 2}
	root.Right.Left = &Node{Value: 4}

	root.Traverse()
}

// 中序遍历
func (node *Node) Traverse() {
	node.TraverseFunc(func(n *Node) {
		n.Print()
	})
	fmt.Println()
}

// 函数式编程实现遍历
func (node *Node) TraverseFunc(f func(*Node)) {
	if node == nil {
		return
	}
	node.Left.TraverseFunc(f)
	f(node)
	node.Right.TraverseFunc(f)
}
